<?php
$catid=request_var('catid',0);
$cat=new Category($catid);
$keyword=request_var('searchKeyword','',true);
?>
<script type="text/javascript">
	$(document).ready(function(){
		$('#topSearchForm').attr('action','index.php?p=cat&catid=<?php echo $catid; ?>');
		$('#searchKeyword').attr('value','<?php echo $keyword; ?>');
	});
</script>
<div class="container">
    <div id="main-content-wrapper">
        <div class="breadcrumb">
		<?php
			for($j=0;$j<count($cat->path);$j++)
				echo '<a href="index.php?p=cat&catid='.$cat->path[$j]['id'].'">'.$cat->path[$j]['catname'].'</a><span>&nbsp;&nbsp;&rsaquo;&nbsp;&nbsp;</span>';
			echo '<a href="index.php?p=cat&catid='.$cat->data['id'].'">'.$cat->data['catname'].'</a><span>&nbsp;&nbsp;&rsaquo;&nbsp;&nbsp;</span>';
			if($keyword!='') echo '<a href="#">Kết quả tìm kiếm</a><span>&nbsp;&nbsp;&rsaquo;&nbsp;&nbsp;</span>';
			$page=request_var('pg',1);
			$href=' href="index.php?p=cat&catid=';
			if($keyword=='') $items=$cat->GetAllItems(true,$page);
			else
			{
				$items=$cat->Search($keyword);
				$href=' href="index.php?p=cat&searchKeyword='.urlencode ($keyword).'catid=';
			}
		?>
        </div>

        <div id="title">            
	        <h1>
			<?php 
				echo $cat->data['catname']; 
				if($keyword!='') echo ' » Kết quả tìm kiếm';
			?>
			</h1>
        </div>
        <div id="rightcontent">
            
            <div class="pagination right">
                <a class="first" <?php if($page!=1) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'"'; ?>>First</a>
				<a class="previous"<?php if($page!=1) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.($page-1) . '"'; ?>>Previous</a>
				<?php
					$from_p=$page-2>0?$page-2:1;
					$to_p=$page+2<$cat->page_num?$page+2:$cat->page_num;
					for($i=$from_p;$i<=$to_p;$i++)
					{
						echo '<a '.($i==$page?' class="active"':' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.$i.'"').'>'.$i.'</a>';
					}
					if($page+2<$cat->page_num) echo '<a class="break">...</a>';
				?>
				<a class="next"<?php if($page!=$cat->page_num) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.($page+1).'"'; ?>>Next</a>
				<a class="last"<?php if($page!=$cat->page_num) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.$cat->page_num.'"'; ?>>Last</a>

            </div>

            <div class="clear"></div>
            <div class="clearfix">
			<?php
				$i=0;
				foreach($items as $item)
				{
					$i++;
					echo '<div class="pitem'.($i%4==0?' last':'').'">
						<a href="index.php?p=item&tag='.$item['tag'].'&id='.$item['id'].'"><img src="'.$item['img'].'" alt="'.$item['name'].'" width="184px"/></a>
						<p class= "name"><a href= "index.php?p=item&tag='.$item['tag'].'&id='.$item['id'].'"><span>'.$item['name'].'</span></a></p>
						<p><span class="label">Giá:</span><span class="price">'.$item['price'].'</span> VND</p>';
					if($item['tag']!='') echo '<p><span class="label">Mã SP:</span><span class="code">'.$item['tag'] . ' - '. $item['id'].'</span></p>';
					echo '</div>';
				}
			?>
            </div>
            <div class="clear"></div>
            <div class="pagination right">
                <a class="first" <?php if($page!=1) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'"'; ?>>First</a>
				<a class="previous"<?php if($page!=1) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.($page-1) . '"'; ?>>Previous</a>
				<?php
					$from_p=$page-2>0?$page-2:1;
					$to_p=$page+2<$cat->page_num?$page+2:$cat->page_num;
					for($i=$from_p;$i<=$to_p;$i++)
					{
						echo '<a '.($i==$page?' class="active"':' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.$i.'"').'>'.$i.'</a>';
					}
					if($page+2<$cat->page_num) echo '<a class="break">...</a>';
				?>
                
				<a class="next"<?php if($page!=$cat->page_num) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.($page+1).'"'; ?>>Next</a>
				<a class="last"<?php if($page!=$cat->page_num) echo ' href="index.php?p=cat&catid='.$cat->data['id'].'&pg='.$cat->page_num.'"'; ?>>Last</a>

            </div>
        </div>
        <div class="left">
            <div class="boxtiny">		        
		        <div class="body">
					<div class="side-categories">
						<ul>
						<?php
							foreach($cat->sub as $sub)
							{
								echo '<li><a href="index.php?p=cat&catid='.$sub['id'].'">'.$sub['catname'].'</a></li>';
							}
						?>
						</ul>
					</div>
				</div>
	        </div>
        </div>      
        <div class="clear"></div>

    </div>
</div>